home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Roberto Ortiz <74011.3205@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Pointers to member functions HOW?
- Date: Wed, 24 Jan 1996 13:46:28 -0400
- Organization: CompuServe Incorporated
- Message-ID: <31067074.6B53@compuserve.com>
- NNTP-Posting-Host: dd60-130.compuserve.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (Win95; I)
-
- I've been attempting for some time now to declare a pointer to a class'
- memeber function. I have no problem with pointers to regular functions, but
- with class members, I run into one of two problems:
-
- a) I can't assign the function to the pointer.
- b) I can't call the pointer as a function.
-
- whenever I solve one, I get the other.
-
- Here's some example code in Borland C++:
-
- #include <stdio.h>
-
- void RegularFunction(int iVal)
- {
- printf("[%d]\n", iVal);
- };
-
- class TTest {
- public:
- void MemberFunction(int iVal)
- {
- printf("[%d]\n", iVal);
- };
- };
-
- void main()
- {
- typedef void (TFuncVoidInt)(int);
-
- TFuncVoidInt *Func0;
- TFuncVoidInt *Func1;
-
- void (TTest::*Func2)(int);
-
- Func0 = RegularFunction;
- Func1 = TTest::MemberFunction;
- Func2 = &TTest::MemberFunction;
-
- Func0(100);
- Func1(100);
- Func2(100);
- };
-
- with this code, I get the following compiler messages:
-
- Compiling PTEST.CPP:
- Error PTEST.CPP 26: Cannot convert 'void (TTest::*)(int)' to 'void (*)(int)'
- in function main()
- Error PTEST.CPP 31: Call of nonfunction in function main()
- function main()
-
- Any suggestions?
-